package quoridor;

import java.io.*;
import java.util.Scanner;

public class QuoridorClient implements Client {
	static final int NUM_OF_PLAYERS = 2;
	
	public QuoridorClient () {
	}

	private void loadGame(int playerAType, int playerBType) throws IOException {
		Scanner INPUT = new Scanner (System.in);
		DecisionMakerType[] decisionMakers = selectDecisionMaker(playerAType, playerBType);
		Game newQuoridorGame;
		BufferedReader fIn;
		String fName;
		String fileContent;

		try {
			//TODO
			System.out.println("Please enter file name");
			INPUT.hasNext();
			fName = INPUT.next();
			fIn = new BufferedReader(new FileReader(fName));
		} catch (Exception exception) {
			System.out.println("File not found. Exit");
			return;
		}
		fileContent = fIn.readLine();
		newQuoridorGame = new QuoridorGame(decisionMakers[0], decisionMakers[1], fileContent);
		newQuoridorGame.playGame();
	}

	private void newGame(int playerAType, int playerBType) {
		Game newQuoridorGame;
		
		DecisionMakerType[] decisionMakers = selectDecisionMaker(playerAType, playerBType);
		newQuoridorGame = new QuoridorGame(decisionMakers[0], decisionMakers[1]);
		newQuoridorGame.playGame();
	}
	
	
	private DecisionMakerType [] selectDecisionMaker(int playerAType, int playerBType) {
		DecisionMakerType[] newDecMakerType = new DecisionMakerType[NUM_OF_PLAYERS];
		switch (playerBType) {
			case 1:
				newDecMakerType[0] = DecisionMakerType.HUMAN;
				break;
			case 2:
				newDecMakerType[0] = DecisionMakerType.DUMB_AI;
				break;
			case 3:
				newDecMakerType[0] = DecisionMakerType.SMART_AI;
				break;
			case 4:
				newDecMakerType[0] = DecisionMakerType.RANDOM_AI;
				break;
			default:
				return null;
		}
		
		switch (playerAType) {
			case 1:
				newDecMakerType[1] = DecisionMakerType.HUMAN;
				break;
			case 2:
				newDecMakerType[1] = DecisionMakerType.DUMB_AI;
				break;
			case 3:
				newDecMakerType[1] = DecisionMakerType.SMART_AI;
				break;
			case 4:
				newDecMakerType[1] = DecisionMakerType.RANDOM_AI;
				break;
			default:
				return null;
		}
		
		return newDecMakerType;
	}
	
	public void run() throws IOException {
		// create the QuoridorDisplay
		QuoridorDisplay.make(DisplayType.TEXT_DISPLAY);
		int [] gameParam = QuoridorDisplay.instance().initialSetup();
		GameType theGameType = GameType.values()[gameParam[0] - 1];

		switch (theGameType)
		{
			case NEWGAME:
				newGame(gameParam[1], gameParam[2]);
				break;
			case LOADGAME:
				loadGame(gameParam[1], gameParam[2]);
				break;
			case EXITGAME:
				break;
			default:
				// should never get into default
				throw new IllegalStateException ("something is wrong");
		}
	}
}
